package com.xfhy.login;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.xfhy.login.data.AccountUtils;
import com.xfhy.login.data.UserInfo;

/**
 * 登录
 * Activity 添加注解 Route，其中 path 是跳转的路径，这里的路径需要注意的是至少需要有两级，/xx/xx
 */
@Route(path = "/account/login")
public class LoginActivity extends AppCompatActivity {

    private TextView mTvLoginState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        updateLoginState();
    }

    private void initView() {
        mTvLoginState = findViewById(R.id.tv_login_state);
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        findViewById(R.id.btn_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginShare();
            }
        });
        findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
    }

    private void login() {
        AccountUtils.sUserInfo = new UserInfo("张三", "admin");
        updateLoginState();
    }

    @SuppressLint("SetTextI18n")
    private void updateLoginState() {
        mTvLoginState.setText("这里是登录界面：" + (AccountUtils.sUserInfo == null ? "未登录" : AccountUtils.sUserInfo.getName()));
    }

    private void exit() {
        AccountUtils.sUserInfo = null;
        updateLoginState();
    }

    private void loginShare() {
        ARouter.getInstance().build("/share/share").withString("share_content", "分享数据到微博").navigation();
    }
}
